<!-- 我的弹窗 -->

<template>
  <WebModal path="backup" title="选择备品" :onSubmit="toChoose">
    <BackupSearch />
    <BackupTable class="marginBottom" />
  </WebModal>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'BackupModal',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixWeb('deal', 'deal_'), CORE.mixWeb('backup', 'backup_')],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听备品弹窗
     */
    backup_visible(visible) {
      visible ? this.toReset() : this.toClear()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 提交选择
     */
    toChoose() {
      const { repair } = this.deal_one
      const oldList = repair.workOrderPreAssetDto || []
      repair.workOrderPreAssetDto = this.backup_param.rowList.map(({ id, stockNum, spareDto: backup }) => ({
        key: id,
        workOrderId: this.one.id,
        assetId: backup.id,
        assetCode: backup.spareCode,
        assetName: backup.spareName,
        modelId: backup.spareTypeId,
        modelCode: backup.spareTypeCode,
        modelName: backup.spareTypeName,
        unit: backup.unitName,
        unitPrice: backup.price * 100,
        totalPrice: backup.price * 100,
        stockNum,
        number: (oldList.find(({ key }) => key === id) || {}).number || 1,
      }))
      this.backup_closeModal()
      setTimeout(() => this.backup_setOne(), 300)
    },

    /**
     * 重置选择
     */
    toReset() {
      this.backup_param.rowList = this.deal_one.repair.workOrderPreAssetDto.map((item) => ({
        id: item.key,
        spareDto: {
          id: item.assetId,
          spareCode: item.assetCode,
          spareName: item.assetName,
          spareTypeId: item.modelId,
          spareTypeCode: item.modelCode,
          spareTypeName: item.modelName,
          unitName: item.unit,
          price: item.unitPrice / 100,
          stockNum: item.stockNum,
        },
      }))
      this.backup_param.idList = this.backup_param.rowList.map(({ id }) => id)
    },

    /**
     * 清空选择
     */
    toClear() {
      this.backup_param.rowList = []
      this.backup_param.idList = []
    },
  },
}
</script>
